
/*
数据库Realm(ANDROID IOS LINUX MACOS WINDOWS),不支持web端
realm不支持分页, objectBox支持分页

https://www.mongodb.com/docs/realm/sdk/flutter/
https://pub.dev/packages/realm/install

dependencies:
  realm: ^0.11.0+rc
import 'package:realm/realm.dart';


使用方法:
注意:变更数据表结构(如:增加主键)有可能导致安装运行失败,解决:删除app重新安装即可;

1.创建dart文件: bean_db_realm.dart, 使用@RealmModel()添加定义的realm对应的实体类,
注意:需要添加对应生成的文件(.g.dart):  part 'bean_db_realm.g.dart';
如:
import 'package:realm/realm.dart';
part 'bean_db_realm.g.dart';
@RealmModel()
class _Car {
  @PrimaryKey()  //注意:主键只能是String或int类型,如果是int类型,也不会自动递增
  int? id;
  late String make;
  String? model;
  int? kilometers = 500;
  _Person? owner;
}

@RealmModel()
class _Person {
  late String name;
  int age = 1;
}

2.使用命令生成realm文件(在对应的开发项目目录下):
flutter pub run realm generate
或  flutter pub run realm generate --watch
(这时看到bean_db_realm.dart的同目录下生产了bean_db_realm.g.dart文件)


3.初始化realm(自定义数据库名称,适用同时使用多个数据库):
import 'package:path/path.dart' as path;
late Realm realm;
Configuration.defaultRealmName = "myRealmName.realm";
Configuration.defaultRealmPath = path.join(Configuration.defaultStoragePath,Configuration.defaultRealmName);
final config = Configuration.local([Car.schema, Person.schema]);
realm = Realm(config);


4.增删改查:

写入:
realm.writeAsync(() {
   Car car=Car("Tesla", model: "Model Y", kilometers: 1);
   realm.add(car);
});

修改:
realm.write(() {
  car.price = 20;
  car.name = "Special Pencil";
});

删除整个表(多个):
realm?.writeAsync((){
  RealmResults<BeanOfflineDBGood>? items = realm?.all<BeanOfflineDBGood>();
  realm?.deleteMany(items!.toList());
});

删除1个item:
realm.writeAsync(() {
  realm?.delete(iitem);
});

删除整个表数据:
realm.writeAsync(() {
  realm.deleteAll<Person>();
});


查找全部:
var cars = realm.all<Car>();
print("There are ${cars.length} cars in the Realm.");
var indexedCar = cars[0];

条件查找:
var filteredCars = realm.all<Car>().query("make == 'Tesla'");
print('Found ${filteredCars.length} Tesla cars');

//通过主键查找:
var itemByKey = realm.find<Car>(0);


//5. Close the realm
realm.close();
Realm.shutdown();

 */

abstract class XRealmDBUtils{



}
